package com.carleasoft.mps.cms.business.personal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.personal.bean.entity.PersonalCenterEntity;
import com.carleasoft.mps.cms.business.personal.bean.po.PersonalCenterPo;
import com.carleasoft.mps.cms.business.personal.dao.PersonalCenterMapper;
import com.carleasoft.mps.cms.business.personal.service.PersonalCenterService;
import com.carleasoft.mps.cms.business.write.bean.entity.LeadingCadreEntity;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: yangHao
 * @createDate:2023/6/14
 * @description:
 */
@Service
public class PersonalCenterServiceImpl extends ServiceImpl<PersonalCenterMapper, PersonalCenterEntity> implements PersonalCenterService {


    @Override
    public Page<PersonalCenterEntity> personalCenter( Page page,PersonalCenterPo personalCenterPo) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        QueryWrapper<PersonalCenterEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("TYPE", personalCenterPo.getType());
            queryWrapper.eq("RESOURCE_TYPE", personalCenterPo.getResourceType());
        queryWrapper.like("USER_CODE", user.getUserCode());
        return this.page(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean isRead(String code) {
        return this.remove(Wrappers.<PersonalCenterEntity>lambdaQuery()
                .eq(PersonalCenterEntity::getCode, code));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean savePersonalCenter(PersonalCenterEntity personalCenterEntity) {
        if (personalCenterEntity.getType()!=1){
            personalCenterEntity.setCode(IdWorker.getIdStr());
        }
        personalCenterEntity.setIsRead(1);
        personalCenterEntity.setCreateTime(LocalDateTime.now());
        return this.save(personalCenterEntity);
    }
}
